package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormDef;
import com.ccp.dev.form.model.FormTable;
import com.ccp.dev.form.service.FormDefService;
import com.ccp.dev.form.service.FormTableService;
import com.ccp.dev.workflow.bpmconst.BpmConst;
import com.ccp.dev.workflow.bpmutil.BpmUtil;
import com.ccp.dev.workflow.model.ActDefModel;
import com.ccp.dev.workflow.model.BpmNodeSet;
import com.ccp.dev.workflow.service.ActDefModelService;
import com.ccp.dev.workflow.service.BpmNodeSetService;
import com.ccp.dev.workflow.service.BpmService;
import org.apache.commons.text.StringEscapeUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:
 * 节点设置控制层
 *
 * @author :wangcheng
 * Date: 2019-06-26
 */
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmNodeSet")
@Controller
@Action(ownermodel = SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmNodeSetController extends SysBaseController {
    @Resource
    private BpmService bpmService;
    @Resource
    private ActDefModelService actDefModelService;
    @Resource
    private BpmNodeSetService bpmNodeSetService;
    @Resource
    private FormDefService formDefService;
    @Resource
    private FormTableService formTableService;
    @Resource
    private RepositoryService repositoryService;

    /**
     * 跳转流程选择器页面
     *
     * @param request http请求
     * @param model   前台mvc容器
     * @return 选择器页面
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/selector")
    @Action(description = "流程节点选择器", detail = "跳转流程选择器页面")
    public String viewSelector(HttpServletRequest request, Model model) {
        String modelKey = RequestUtil.getString(request, "modelKey");
        model.addAttribute("modelKey", modelKey);
        return getAutoView();
    }

    /**
     * 节点设置列表页面
     *
     * @param model   前台mvc容器
     * @param request http请求
     * @return 节点设置列表页面
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "节点设置List页面", detail = "查看节点设置分页列表<#if success>成功<#else>失败</#if>")
    public String viewList(Model model, HttpServletRequest request) {
        String modelId = RequestUtil.getString(request, "modelId");
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId");

        // 是否是新的流程，或者重新设置并且没有绑定任何表单的流程
        String isNew = "yes";
        ActDefModel actDefModel = actDefModelService.getOneById(modelId);
        if(actDefModel!=null){
            SysAuditThreadLocalHolder.putParamerter("success", true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        String procDefId = actDefModel.getProcDefId();
        List<BpmNodeSet> bpmNodeSetList;
        if (StringUtil.isEmpty(parentProcDefId)) {
            bpmNodeSetList = bpmNodeSetService.queryByProcDefId(procDefId);
        } else {
            bpmNodeSetList = bpmNodeSetService.queryByProcDefIdAndParentProcDefId(procDefId, parentProcDefId, false);
        }

        // 全局表单设置
        BpmNodeSet globalForm;
        // 实例表单设置
        BpmNodeSet bpmForm;
        if (StringUtil.isEmpty(parentProcDefId)) {
            globalForm = bpmNodeSetService.getBySetType(procDefId, BpmNodeSet.SETTYPE_GLOABLEFORM);
            bpmForm = bpmNodeSetService.getBySetType(procDefId, BpmNodeSet.SETTYPE_BPMFORM);
        } else {
            globalForm = bpmNodeSetService.getBySetType(procDefId, BpmNodeSet.SETTYPE_GLOABLEFORM, parentProcDefId);
            bpmForm = bpmNodeSetService.getBySetType(procDefId, BpmNodeSet.SETTYPE_BPMFORM, parentProcDefId);
        }

        // 判断全局在线表单是否有子表
        if (BeanUtils.isNotEmpty(globalForm)) {
            FormDef formDef = formDefService.getDefaultPublishedByFormKey(globalForm.getFormKey());
            if (BeanUtils.isNotEmpty(formDef) && StringUtil.isNotEmpty(formDef.getTableId())) {
                List<FormTable> subTableList = formTableService.getSubTableByMainTableId(formDef.getTableId());
                if (BeanUtils.isNotEmpty(subTableList)) {
                    globalForm.setMainTableId(formDef.getTableId());
                    globalForm.setExistSubTable(new Short("1"));
                } else {
                    globalForm.setMainTableId("01");
                    globalForm.setExistSubTable(new Short("0"));
                }
            }
        }

        // 判断节点表单是否有子表
        for (BpmNodeSet bpmNodeSet : bpmNodeSetList) {
            FormDef formDef = formDefService.getDefaultPublishedByFormKey(bpmNodeSet.getFormKey());
            if (BeanUtils.isNotEmpty(formDef) && StringUtil.isNotEmpty(formDef.getTableId())) {
                if (StringPool.YES.equals(isNew)) {
                    isNew = StringPool.NO;
                }
                List<FormTable> subTableList = formTableService.getSubTableByMainTableId(formDef.getTableId());
                if (BeanUtils.isNotEmpty(subTableList)) {
                    bpmNodeSet.setExistSubTable(new Short("1"));
                    bpmNodeSet.setMainTableId(formDef.getTableId());
                } else {
                    bpmNodeSet.setExistSubTable(new Short("0"));
                    bpmNodeSet.setMainTableId("01");
                }
            }
        }

        //当 bpmNodeSetList globalForm bpmForm 三个都为空时 表示没有绑定表单
        if (StringPool.YES.equals(isNew) && BeanUtils.isNotEmpty(globalForm) || BeanUtils.isNotEmpty(bpmForm)) {
            isNew = StringPool.NO;
        }

        model.addAttribute("bpmNodeSetList", bpmNodeSetList);
        model.addAttribute("actDefModel", actDefModel);
        model.addAttribute("globalForm", globalForm);
        model.addAttribute("bpmForm", bpmForm);
        model.addAttribute("isNew", isNew);
        model.addAttribute("parentProcDefId", parentProcDefId);


        return "workflow/actDefModel/formSet";

    }

    /**
     * 保存流程表单设置
     *
     * @param request http请求
     * @return 跳转路径
     */
    @RequestMapping("/save")
    @Action(description = "保存流程表单设置",
            detail = "设置流程定义${modelId}的节点表单及跳转方式<#if success>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData save(HttpServletRequest request) {
        ResultData resultData;
        String modelId = RequestUtil.getString(request, "modelId");
        String beforeHandlerGlobal = RequestUtil.getString(request, "beforeHandlerGlobal");
        String afterHandlerGlobal = RequestUtil.getString(request, "afterHandlerGlobal");

        // 验证前置处理器是否合法
        if (StringUtil.isNotEmpty(beforeHandlerGlobal)) {
            resultData = validHandler(beforeHandlerGlobal);
            if (resultData.getCode() != 0) {
                return resultData;
            }
        }

        // 验证后置处理器是否合法
        if (StringUtil.isNotEmpty(afterHandlerGlobal)) {
            resultData = validHandler(afterHandlerGlobal);
            if (resultData.getCode() != 0) {
                return resultData;
            }
        }

        // 父流程定义id
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId", "");
        ActDefModel actDefModel = actDefModelService.getOneById(modelId);
        String procDefId = actDefModel.getProcDefId();
        String[] nodeIds = request.getParameterValues("nodeId");
        String[] nodeNames = request.getParameterValues("nodeNames");
        String[] formTypes = request.getParameterValues("formType");
        String[] formKeys = request.getParameterValues("formKey");
        String[] formUrls = request.getParameterValues("formUrl");
        String[] formDefNames = request.getParameterValues("formDefName");
        String[] beforeHandlers = request.getParameterValues("beforeHandler");
        String[] afterHandlers = request.getParameterValues("afterHandler");

        // 获取流程表单设置数据
        Map<String, BpmNodeSet> nodeMap;
        if (StringUtil.isEmpty(parentProcDefId)) {
            nodeMap = bpmNodeSetService.getMapByProcDefId(procDefId);
        } else {
            // 子流程的节点数据
            nodeMap = bpmNodeSetService.getMapByProcDefIdAndParentProcDefId(procDefId, parentProcDefId, true);
        }

        List<BpmNodeSet> nodeSetList = new ArrayList<>();
        for (int i = 0; i < nodeIds.length; i++) {
            String nodeId = nodeIds[i];
            BpmNodeSet nodeSet = new BpmNodeSet();
            if (nodeMap.containsKey(nodeId)) {
                nodeSet = nodeMap.get(nodeId);
                //设置原有的表单key,用于删除之前设置表单节点的权限
                if (StringUtil.isNotEmpty(nodeSet.getFormKey())) {
                    nodeSet.setOldFormKey(nodeSet.getFormKey());
                }
            }
            nodeSet.setNodeId(nodeId);
            nodeSet.setProcDefId(procDefId);
            nodeSet.setNodeName(nodeNames[i]);

            short formType = Short.parseShort(formTypes[i]);

            // 验证流程节点的前后置处理器是否合法
            String beforeHandler = beforeHandlers[i];
            if (StringUtil.isNotEmpty(beforeHandler)) {
                resultData = validHandler(beforeHandler);
                if (resultData.getCode() != 0) {
                    return resultData;
                }
            }
            String afterHandler = afterHandlers[i];
            if (StringUtil.isNotEmpty(afterHandler)) {
                resultData = validHandler(afterHandler);
                if (resultData.getCode() != 0) {
                    return resultData;
                }
            }
            beforeHandler = getHandler(beforeHandler);
            afterHandler = getHandler(afterHandler);

            String formUrl = formUrls[i];
            nodeSet.setFormType(formType);

            // 没有选择表单
            switch (formType) {
                case -1:
                    nodeSet.setFormUrl("");
                    nodeSet.setDetailUrl("");
                    nodeSet.setFormKey("");
                    nodeSet.setFormDefName("");
                    break;
                case 0: {
                    // 在线表单
                    nodeSet.setFormUrl("");
                    nodeSet.setDetailUrl("");

                    String formKey;
                    if (StringUtil.isNotEmpty(formKeys[i])) {
                        formKey = formKeys[i];
                        nodeSet.setFormKey(formKey);
                        nodeSet.setFormDefName(formDefNames[i]);
                    } else {
                        nodeSet.setFormKey("");
                        nodeSet.setFormDefName("");
                        nodeSet.setFormType((short) -1);
                    }
                    break;
                }
                default: {
                    // url表单
                    String formKey;
                    if (StringUtil.isNotEmpty(formKeys[i])) {
                        formKey = formKeys[i];
                        nodeSet.setFormKey(formKey);
                        nodeSet.setFormDefName(formDefNames[i]);

                    } else {
                        nodeSet.setFormKey("");
                        nodeSet.setFormDefName("");

                    }
                    nodeSet.setFormUrl(formUrl);
                    break;
                }
            }

            // 添加前后置处理器
            nodeSet.setBeforeHandler(beforeHandler);
            nodeSet.setAfterHandler(afterHandler);

            // 添加跳转类型
            String[] jumpType = request.getParameterValues("jumpType_" + nodeId);
            if (jumpType != null) {
                nodeSet.setJumpType(StringUtil.getArrayAsString(jumpType));
            } else {
                nodeSet.setJumpType("");
            }

            // 添加意见处理以及隐藏路径类型
            String isHideOption = request.getParameter("isHideOption_" + nodeId);
            String isRequired = request.getParameter("isRequired_" + nodeId);
            String isPopup = request.getParameter("isPopup_" + nodeId);
            String canUploadFile= request.getParameter("canUploadFile_" + nodeId);
            String opinionField = request.getParameter("opinionField_" + nodeId);
            String isHidePath = request.getParameter("isHidePath_" + nodeId);
            String opinionHtml = request.getParameter("opinionHtml_" + nodeId);

            if (StringUtil.isNotEmpty(isHideOption)) {
                nodeSet.setIsHideOption(BpmNodeSet.HIDE_OPTION);
            } else {
                nodeSet.setIsHideOption(BpmNodeSet.NOT_HIDE_OPTION);
            }
            if (StringUtil.isNotEmpty(canUploadFile)) {
                nodeSet.setCanUploadFile(BpmNodeSet.CAN_UPLOAD_FILE);
            } else {
                nodeSet.setCanUploadFile(BpmNodeSet.CAN_NOT_UPLOAD_FILE);
            }
            if (StringUtil.isNotEmpty(isRequired)) {
                nodeSet.setIsRequired(BpmNodeSet.IS_REQUIRED);
            } else {
                nodeSet.setIsRequired(BpmNodeSet.NOT_IS_REQUIRED);
            }
            if (StringUtil.isNotEmpty(isPopup)) {
                nodeSet.setIsPopup(BpmNodeSet.IS_POPUP);
            } else {
                nodeSet.setIsPopup(BpmNodeSet.NOT_IS_POPUP);
            }

            nodeSet.setOpinionField(opinionField);

            if (StringUtil.isNotEmpty(isHidePath)) {
                nodeSet.setIsHidePath(BpmNodeSet.HIDE_PATH);
            } else {
                nodeSet.setIsHidePath(BpmNodeSet.NOT_HIDE_PATH);
            }

            if (StringUtil.isNotEmpty(opinionHtml)) {
                nodeSet.setOpinionHtml((short) 1);
            } else {
                nodeSet.setOpinionHtml((short) 0);
            }
            nodeSet.setSetType(BpmNodeSet.SETTYPE_TASKNODE);
            nodeSet.setParentProcDefId(parentProcDefId);
            nodeSetList.add(nodeSet);
        }

        // 获取全局设置
        List<BpmNodeSet> globalBpmNodeSetList = getGlobalBpm(request, actDefModel);
        nodeSetList.addAll(globalBpmNodeSetList);
        try {
            bpmNodeSetService.save(procDefId, nodeSetList, parentProcDefId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success");
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataSuccess("operation.failure", 1, e.getMessage());
        }
    }

    /**
     * 从request中构建全局和流程实例表单数据。
     *
     * @param request     http请求
     * @param actDefModel 流程定义模型
     * @return 节点设置信息列表
     */
    private List<BpmNodeSet> getGlobalBpm(HttpServletRequest request, ActDefModel actDefModel) {
        List<BpmNodeSet> list = new ArrayList<>();
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId", "");
        int globalFormType = RequestUtil.getInt(request, "globalFormType");

        if (globalFormType >= 0) {
            // 全局表单信息
            String defaultFormKey = RequestUtil.getString(request, "defaultFormKey");
            String defaultFormName = RequestUtil.getString(request, "defaultFormName");
            // 全局前后置处理器
            String beforeHandlerGlobal = RequestUtil.getString(request, "beforeHandlerGlobal");
            String afterHandlerGlobal = RequestUtil.getString(request, "afterHandlerGlobal");
            // 手机端表单设置
            String defaultMobileFormKey = RequestUtil.getString(request, "defaultMobileFormKey");

            beforeHandlerGlobal = getHandler(beforeHandlerGlobal);
            afterHandlerGlobal = getHandler(afterHandlerGlobal);
            // 离线表单url
            String formUrlGlobal = RequestUtil.getString(request, "formUrlGlobal");
            // 构造流程表单设置对象
            BpmNodeSet bpmNodeSet = new BpmNodeSet();
            bpmNodeSet.setProcDefId(actDefModel.getProcDefId());
            bpmNodeSet.setFormKey(defaultFormKey);
            bpmNodeSet.setFormDefName(defaultFormName);
            bpmNodeSet.setFormUrl(formUrlGlobal);
            bpmNodeSet.setBeforeHandler(beforeHandlerGlobal);
            bpmNodeSet.setAfterHandler(afterHandlerGlobal);
            bpmNodeSet.setFormType((short) globalFormType);
            bpmNodeSet.setSetType(BpmNodeSet.SETTYPE_GLOABLEFORM);
            bpmNodeSet.setParentProcDefId(parentProcDefId);

            bpmNodeSet.setMobileFormKey(defaultMobileFormKey);

            if (globalFormType == BpmConst.FORM_TYPE_ONLINE) {
                if (StringUtil.isNotEmpty(defaultFormKey)) {
                    list.add(bpmNodeSet);
                }
            } else if (StringUtil.isNotEmpty(formUrlGlobal)) {
                bpmNodeSet.setFormKey(null);
                list.add(bpmNodeSet);
            }
        }
        int bpmFormType = RequestUtil.getInt(request, "bpmFormType");
        if (bpmFormType >= 0) {
            String bpmFormKey = RequestUtil.getString(request, "bpmFormKey");
            String bpmFormName = RequestUtil.getString(request, "bpmFormName");
            String bpmFormUrl = RequestUtil.getString(request, "bpmFormUrl");

            BpmNodeSet bpmNodeSet = new BpmNodeSet();
            bpmNodeSet.setProcDefId(actDefModel.getProcDefId());

            if (StringUtil.isNotEmpty(bpmFormKey)) {
                bpmNodeSet.setFormKey(bpmFormKey);
                bpmNodeSet.setFormDefName(bpmFormName);
            }
            bpmNodeSet.setFormUrl(bpmFormUrl);
            bpmNodeSet.setFormType((short) bpmFormType);
            bpmNodeSet.setSetType(BpmNodeSet.SETTYPE_BPMFORM);
            bpmNodeSet.setParentProcDefId(parentProcDefId);
            list.add(bpmNodeSet);
        }
        return list;
    }

    /**
     * 验证handler
     * 输入格式为 serviceId + "." + 方法名
     *
     * @param handler 处理器名称
     * @return 结果封装类
     */
    @RequestMapping("/validHandler")
    @Action(description = "节点设置验证handler", detail = "验证处理器<#if success>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData validHandler(@RequestParam String handler) {
        int rtn = BpmUtil.isHandlerValid(handler);
        String msg;
        switch (rtn) {
            case 0:
                msg = "valid.input";
                break;
            case -1:
                msg = "invalid.input";
                break;
            case -2:
                msg = "no.class.service";
                break;
            case -3:
                msg = "no.related.method";
                break;
            default:
                msg = "other.faults";
                break;
        }

        if (rtn != 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataError(msg);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataSuccess(msg);
        }
    }

    /**
     * 对流程前后置处理器进行设置
     *
     * @param handler 处理器
     * @return 规范化处理器字符串
     */
    private String getHandler(String handler) {
        if (StringUtil.isEmpty(handler) || !handler.contains(StringPool.DOT)) {
            handler = "";
        }
        return handler;
    }

    /**
     * 获取流程节点人员选择器设置
     *
     * @param request http请求
     * @return 人员选择器设置页面
     */
    @RequestMapping("/flowScope")
    @Action(description = "获取流程节点", detail = "获取流程节点<#if fl>成功<#else>失败</#if>")
    public String flowScope(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId");
        BpmNodeSet bpmNodeSet = bpmNodeSetService.getScopeByNodeIdAndProcDefId(nodeId, procDefId, parentProcDefId);
        if(bpmNodeSet!=null){
            SysAuditThreadLocalHolder.putParamerter("success", true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        model.addAttribute("bpmNodeSet", bpmNodeSet);
        return "workflow/actDefModel/flowScope";
    }

    /**
     * 为流程节点设置选择器的的使用范围
     *
     * @param request http请求
     * @return 操作结果封装
     */
    @RequestMapping("/saveScope")
    @ResponseBody
    @Action(description = "保存流程节点选择器范围设置", detail = "为流程节点设置选择器的使用范围<#if success>成功<#else>失败</#if>")
    public ResultData saveScope(HttpServletRequest request) {
        String setId = RequestUtil.getString(request, "setId");
        String scope = RequestUtil.getString(request, "scope");
        scope = StringEscapeUtils.unescapeHtml4(scope);
        try {
            bpmNodeSetService.updateScopeById(setId, scope);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success");
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure", 1, e.getMessage());
        }
    }

    /**
     * 节点设置--流程任务分发汇总配置跳转弹窗
     *
     * @param request http请求
     * @param model 前台mvc容器
     * @return 分发汇总配置页面
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/flowForkJoin")
    @Action(description = "节点设置——流程任务分发汇总配置跳转弹窗", detail = "节点设置——流程任务分发汇总配置跳转弹窗")
    public String viewFlowForkJoin(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");

        BpmNodeSet bpmNodeSet = bpmNodeSetService.getByProcDefIdNodeId(procDefId, nodeId);
        //回显数据库中对应流程节点的分发汇总配置
        if (bpmNodeSet != null) {
            model.addAttribute("bpmNodeSet", bpmNodeSet);
        }
        //与老平台方法不同，待验证节点信息是否完全
        ActDefModel actDefModel = actDefModelService.getByProcDefId(procDefId);
        BpmnModel bpmnModel = actDefModelService.getBpmnModel(actDefModel);

        //获取移除nodeId节点的其他所有节点
        Map<String, String> nodeMap = bpmService.getTaskNodes(bpmnModel, nodeId);
        model.addAttribute("procDefId", procDefId)
                .addAttribute("nodeId", nodeId)
                .addAttribute("nodeMap", nodeMap);
        return getAutoView();
    }

    /**
     * 节点设置--流程任务分发汇总配置保存
     *
     * @param request http请求
     * @return 节点设置封装结果
     */
    @RequestMapping("/saveFlowForkJoin")
    @ResponseBody
    @Action(description = "节点设置——流程任务分发汇总配置保存",
            detail = "设置流程定义【${procDefId}】节点"
                    + "【${nodeId}】为"
                    + "<#if '0'==nodeType>【普通任务】<#else>【分发任务】,汇总任务名称为【${joinTaskName}】</#if><#if success>成功<#else>失败</#if>")
    public ResultData saveFlowForkJoin(HttpServletRequest request) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        Short nodeType = RequestUtil.getShort(request, "nodeType");
        String joinTaskKey = RequestUtil.getString(request, "joinTaskKey");
        String joinTaskName = RequestUtil.getString(request, "joinTaskName");

        SysAuditThreadLocalHolder.putParamerter("procDefId", procDefId);
        SysAuditThreadLocalHolder.putParamerter("nodeId", nodeId);
        SysAuditThreadLocalHolder.putParamerter("nodeType", nodeType);
        SysAuditThreadLocalHolder.putParamerter("joinTaskName", joinTaskName);

        //获取普通任务节点
        BpmNodeSet bpmNodeSet = bpmNodeSetService.getByProcDefIdNodeIdOrParentProcDefId(nodeId, procDefId, null);
        //获取分发汇总任务节点
        BpmNodeSet joinKeyNodeSet = bpmNodeSetService.getByProcDefIdJoinTaskKey(procDefId, joinTaskKey);
        //查看该汇总是否被其他节点设置了为汇总节点
        if (joinKeyNodeSet != null) {
            if (bpmNodeSet == null || !bpmNodeSet.getSetId().equals(joinKeyNodeSet.getSetId())) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError("bpmNodeSet.joinTaskKey.setting");
            }
        }
        //保存配置
        if (bpmNodeSet != null) {
            bpmNodeSetService.updateJoinTask(bpmNodeSet.getSetId(), nodeType, joinTaskKey, joinTaskName);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("bpmNodeSet.joinTaskKey.set.success");
        }
        SysAuditThreadLocalHolder.putParamerter("success", false);
        return getResultDataError("bpmNodeSet.query.nodeId.failure");
    }

    /**
     * 获取外部子流程绑定的流程对象
     * @param request http请求
     * @return 自留陈给封装
     */
    @RequestMapping("/getCallActivityModel")
    @ResponseBody
    @Action(description = "获取外部子流程绑定的流程", detail = "获取外部子流程绑定的流程")
    public ResultData getCallActivityModel(HttpServletRequest request) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");

        BpmnModel bpmnModel = repositoryService.getBpmnModel(procDefId);
        // 获取绑定的子流程key
        String modelKey = actDefModelService.getCallActivityModelKey(bpmnModel, nodeId);
        if (StringUtil.isEmpty(modelKey)) {
            return getResultDataError("callActivity.has.no.ref.flow");
        }
        ActDefModel actDefModel = actDefModelService.getMainByDefKey(modelKey);
        return getResultDataSuccess("operation.success", actDefModel);
    }
}
